package com.asus.testapplicaion;

import java.util.HashMap;
import java.util.Map;

import com.distimo.sdk.DistimoSDK;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	AdView adView;
	private static final String GA_PROPERTY_ID = "UA-42963175-1";
	private static final String SCREEN_LABEL = "Home Screen";

	// This examples assumes the use of Google Analytics campaign
	// "utm" parameters, like "utm_source"
	private static final String CAMPAIGN_SOURCE_PARAM = "utm_source";

	Tracker mTracker;

	@Override
	public void onStart() {
		super.onStart();
		EasyTracker.getInstance(this).activityStart(this);

		// Add this method.
		// Set screen name on tracker so that all subsequent hits will use this
		// value.
		mTracker.set(Fields.SCREEN_NAME, SCREEN_LABEL);

		// Get the intent that started this Activity.
		Intent intent = this.getIntent();
		Uri uri = intent.getData();

		// Send a screenview using any available campaign or referrer data.
		MapBuilder.createAppView().setAll(getReferrerMapFromUri(uri));
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		DistimoSDK.onCreate(this, "RxgkaKHukFK1qp0F");
		mTracker = GoogleAnalytics.getInstance(this).getTracker(GA_PROPERTY_ID);

		HashMap<String, String> hitParameters = new HashMap<String, String>();
		hitParameters.put(Fields.HIT_TYPE, "appview");
		hitParameters.put(Fields.SCREEN_NAME, "Home Screen");
		mTracker.send(hitParameters);

		setContentView(R.layout.activity_main);
		adView = (AdView) this.findViewById(R.id.adView);
		adView.loadAd(new AdRequest());

		Button btn1 = (Button) this.findViewById(R.id.button1);
		btn1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// May return null if a EasyTracker has not yet been initialized
				// with a
				// property ID.
				EasyTracker easyTracker = EasyTracker
						.getInstance(getBaseContext());
				// MapBuilder.createEvent().build() returns a Map of event
				// fields and values
				// that are set and sent with the hit.
				easyTracker.send(MapBuilder.createEvent("ui_action", // Event
																		// category
																		// (required)
						"button1", // Event action (required)
						"clikc", // Event label
						null) // Event value
						.build());

				Intent intent = new Intent();
				intent.setClass(MainActivity.this, Activity2.class);
				MainActivity.this.startActivity(intent);
				MainActivity.this.finish();

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		try {

			// Inflate the menu; this adds items to the action bar if it is
			// present.
			getMenuInflater().inflate(R.menu.main, menu);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return true;
		}
	}

	@Override
	public void onDestroy() {
		if (adView != null) {
			adView.destroy();
		}
		super.onDestroy();
	}

	@Override
	public void onStop() {
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this); // Add this method.

	}

	/*
	 * Given a URI, returns a map of campaign data that can be sent with any GA
	 * hit.
	 * 
	 * @param uri A hierarchical URI that may or may not have campaign data
	 * stored in query parameters.
	 * 
	 * @return A map that may contain campaign or referrer that may be sent with
	 * any Google Analytics hit.
	 */
	Map<String, String> getReferrerMapFromUri(Uri uri) {

		MapBuilder paramMap = new MapBuilder();

		// If no URI, return an empty Map.
		if (uri == null) {
			return paramMap.build();
		}

		// Source is the only required campaign field. No need to continue if
		// not
		// present.
		if (uri.getQueryParameter(CAMPAIGN_SOURCE_PARAM) != null) {

			// MapBuilder.setCampaignParamsFromUrl parses Google Analytics
			// campaign
			// ("UTM") parameters from a string URL into a Map that can be set
			// on
			// the Tracker.
			paramMap.setCampaignParamsFromUrl(uri.toString());

			// If no source parameter, set authority to source and medium to
			// "referral".
		} else if (uri.getAuthority() != null) {

			paramMap.set(Fields.CAMPAIGN_MEDIUM, "referral");
			paramMap.set(Fields.CAMPAIGN_SOURCE, uri.getAuthority());

		}

		return paramMap.build();
	}

}
